package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.EmployeeMapper;
import com.example.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class EmployeeServiceImpe implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    //增加一个员工
    @Override
    public void save(Employee employee) {
        employeeMapper.insert(employee);
    }

    //查询全部员工信息
    @Override
    public Collection<Employee> getAll() {
        return employeeMapper.getAll();
    }

    //查询员工通过id
    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.getEmployeeById(id);
    }

    //查询员工通过用户名
    @Override
    public Employee getEmployeeByName(String lastName) {
        //创建条件构造器对象
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //给构造器加入条件
        queryWrapper.eq("lastName",lastName);
        //根据条件构造器里的条件进行查询
        List<Employee> employees = employeeMapper.selectList(queryWrapper);
        //查询为空则返回null
        if (!employees.isEmpty()) {
            return employees.get(0);
        }else {
            return null;
        }
    }

    //删除员工通过id
    @Override
    public void delete(Integer id) {
        //员工存在才删除
        if (employeeMapper.selectById(id) != null) {
            QueryWrapper<Employee> queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",id);
            employeeMapper.delete(queryWrapper);
        }
    }

    //修改员工信息
    @Override
    public void update(Employee employee) {
        //员工存在才修改
        if (employeeMapper.selectById(employee.getId()) != null) {
            QueryWrapper<Employee> queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",employee.getId());
            employeeMapper.update(employee,queryWrapper);
        }
    }
}
